﻿Imports System.Globalization
Imports Aricie.DNN.Services.Filtering
Imports System.Web
Imports Dataspring.DNN.Modules.OptInAdvanceEmail

Namespace Aricie.DNN.Services.Url.FriendlyUrl.Providers


    Public Class OptinEmailUrlRewriter
        Implements IUrlRewriterProvider
        Implements IIsActive


        Public Function GetModuleRewrites() As List(Of GroupParamsRewrite) Implements IUrlRewriterProvider.GetRewrites
            Dim toReturn As New List(Of GroupParamsRewrite)


            Dim groupNewsletter As New GroupParamsRewrite("NewsletterTitle", "Title of the current Newsletter", RewriteType.SubPath)
            toReturn.Add(groupNewsletter)
            Return toReturn
        End Function

        Public Function RewriteParams(ByVal groupName As String, ByVal objRewriteType As RewriteType, _
                                      ByVal params As Dictionary(Of UrlParam, String), ByVal behaviour As FriendlierUrlStrategy) As GroupRewriteResult _
                                      Implements IUrlRewriterProvider.RewriteParams
            Dim toReturn As New GroupRewriteResult()
            Select Case groupName.ToLowerInvariant
                Case "newslettertitle"
                    toReturn.RewriteValue = GetNewsletterTitle(behaviour)
            End Select
            Return toReturn
        End Function

        Private Function GetNewsletterTitle(ByVal behaviour As FriendlierUrlStrategy) As String
            Dim toReturn As String = ""

            Dim previousNewsletterController As New OptInPreviousNewsletterController
            Dim previousNewsletter As OptInPreviousNewsletterInfo
            Dim guidCurrent As New Nullable(Of Guid)

            If HttpContext.Current IsNot Nothing Then
                If HttpContext.Current.Session IsNot Nothing Then
                    If HttpContext.Current.Session("CurrentPreviousNewsletterID") IsNot Nothing Then
                        guidCurrent = New Guid(CStr(HttpContext.Current.Session("CurrentPreviousNewsletterID")))
                    End If
                End If

            End If
            If Not guidCurrent.HasValue Then
                Dim mid As Integer = NukeHelper.ModuleController.GetModuleByDefinition(NukeHelper.PortalId, "Opt In - Advanced Email").ModuleID
                If mid <> -1 Then
                    Dim settings As Hashtable = NukeHelper.ModuleController.GetModuleSettings(mid)
                    If settings("Newsletter_SelectID") <> Guid.Empty.ToString Then
                        guidCurrent = New Guid(CStr(settings("Newsletter_SelectID")))
                    End If
                End If
            End If

            If guidCurrent.HasValue Then
                Dim myList As ArrayList = previousNewsletterController.GetPreviousNewsletterByID(guidCurrent)

                ' On affiche la dernière newsletter --- Provisoire - Intégrer au settings la sélection d'une newsletter
                If myList.Count > 0 Then

                    previousNewsletter = DirectCast(myList(0), OptInPreviousNewsletterInfo)
                    'toReturn = behaviour.StringEscaper.EscapeString(previousNewsletter.Subject)
                    toReturn = previousNewsletter.Subject
                End If
            End If


            Return toReturn
        End Function


        Public ReadOnly Property IsActive As Boolean Implements IIsActive.IsActive
            Get
                Return DotNetNuke.Framework.Reflection.CreateType("Dataspring.DNN.Modules.OptInAdvanceEmail.OptInPreviousNewsletterController", True) IsNot Nothing
            End Get
        End Property
    End Class

End Namespace